using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using UnityEngine;
using UnityEngine.UI;
using MyUnityUtils;
using Newtonsoft.Json;


namespace EmailPanel
{
    public class DetailCtrl : MonoBehaviour
    {
        public bool open_dir = false;
        
        public Text detail_title;
        public Text detail_sender;
        public Text detail_time;
        public Text detail_content;
        
        public GameObject detail_attachment_parent_obj;
        public GameObject scroll_content_parent_obj;
        
        public GameObject detail_btn_parent_obj;

        public int current_email_index = -1;

        public EmailFileData email_file_data;

        public AppFileTools.FileObject read_email_file_obj;
        public string key_of_read_email_file_data = "EmailFileData";

        public DataTable item_info_dt;
        public string item_info_file_path = "物品清单.xlsx";

        public Dictionary<int, EmailInfo> email_info_dc;

        public void update_file_obj(int obj_id)
        {
            current_email_index = obj_id;
            if (!email_file_data.read_id_ls.Contains(obj_id))
            {
                email_file_data.read_id_ls.Add(obj_id);
                // BdPackage.BdUtils.log("email_file_data.read_id_ls:", email_file_data.read_id_ls);

                string json_value = BdPackage.BdUtils.conv_obj_to_json(email_file_data);
                if (read_email_file_obj.content_dc.ContainsKey(key_of_read_email_file_data))
                {
                    read_email_file_obj.content_dc[key_of_read_email_file_data] = json_value;
                }
                else
                {
                    read_email_file_obj.content_dc.Add(key_of_read_email_file_data, json_value);
                }
                read_email_file_obj.save();
            }
        }

        private void Awake()
        {
            email_info_dc = new Dictionary<int, EmailInfo>();
            
            read_email_file_obj = new AppFileTools.FileObject("ReadEmails", "EmailPanel");
            string _email_data = read_email_file_obj.content_dc.TryGetValue(key_of_read_email_file_data, 
                out var tmp) ? BdPackage.BdUtils.conv_to_string(tmp) : "";

            if (open_dir)
            {
                read_email_file_obj.open_dir();
            }

            if (BdPackage.BdUtils.not_null(_email_data))
            {
                email_file_data = JsonConvert.DeserializeObject<EmailFileData>(_email_data);
                // BdPackage.BdUtils.log("--- _email_data --- email_file_data.read_id_ls:", email_file_data.read_id_ls);
            }
            else
            {
                email_file_data = new EmailFileData();
                email_file_data.read_id_ls = new List<int>();
            }

            string item_info_f_path = BdPackage.BdTools.get_static_abs_file_path_on_all_platform(item_info_file_path);
            item_info_dt = AppTableUtils.TableUtils.read_table(item_info_f_path, primary_key: "ID");
        }
    }
}
